home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Xconq 7.0d37 / source / kernel / world.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-04  |  15.6 KB  |  438 lines  |  [TEXT/KAHL]

  1. /* Definitions relating to worlds and areas in Xconq.
  2.    Copyright (C) 1987, 1988, 1989, 1991, 1992, 1993, 1994, 1995
  3.    Stanley T. Shebs.
  4.  
  5. Xconq is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.  See the file COPYING.  */
  9.  
  10. typedef struct a_world {
  11.     int circumference;        /* number of cells going around the world */
  12.     int daylength;        /* number of turns in a day */
  13.     int yearlength;        /* number of turns in a year */
  14.     int axialtilt;        /* controls extrema of seasons */
  15.     int firstmidwinter;
  16.     int summernorthpoletemperature;
  17.     int winternorthpoletemperature;
  18.     int summernorthequatortemperature;
  19.     int winternorthequatortemperature;
  20. } World;
  21.  
  22. /* Theoretically, there is no maximum size to Xconq areas, but the minimum
  23.    size is set by mystical properties, and is not negotiable. */
  24.  
  25. #define MINWIDTH 3
  26. #define MINHEIGHT 3
  27.  
  28. /* An "area" is always basically a rectangular array of positions.  The
  29.    hex effect can be achieved by interpreting neighborliness and direction
  30.    differently, but that's all that's needed - storagewise the rect
  31.    array is still the best choice.  All of the "layers" are dynamically
  32.    allocated as needed, to save (considerable!) space. */
  33.  
  34. typedef struct a_area {
  35.     int width, height;        /* size of the area */
  36.     int maxdim;            /* max of the two dims */
  37.     int xwrap;            /* true if x coords wrap around */
  38.     long latitude, longitude;    /* position within whole world */
  39.     long cellwidth;        /* distance across one cell */
  40.     short fullwidth, fullheight; /* size of the area being used for data */
  41.     short fullx, fully;        /* offset within full area to get data from */
  42.     /* Pointers to the various "layers". */
  43.     struct a_unit **units;    /* pointer to units if any */
  44.     char *terrain;        /* terrain type at this spot */
  45.     char **auxterrain;        /* vector of extra types */
  46.     char *peopleside;        /* the overt side alignment of the locals */
  47.     short *features;        /* layer of ids of features */
  48.     short *elevations;        /* layer of elevations */
  49.     short constelev;        /* a constant elevation */
  50.     short **materials;        /* layer of materials in each cell */
  51.     short *temperature;        /* layer of cell temperatures */
  52.     short consttemp;        /* a constant temperature */
  53.     short *clouds;        /* types of clouds in the layer */
  54.     short *cloudbottoms;    /* altitudes of clouds of cloud layer */
  55.     short *cloudheights;    /* heights of clouds in the cloud layer */
  56.     short *winds;        /* layer of force/dir of winds. */
  57.     short constwinds;        /* constant wind */
  58.     /* These layers are for temporary use in calculations. */
  59.     short *tmp1;
  60.     short *tmp2;
  61.     short *tmp3;
  62. } Area;
  63.  
  64. /* Named geographical features. */
  65.  
  66. typedef struct a_feature {
  67.     int type;            /* index of the general type */
  68.     short id;            /* which one this is */
  69.     char *name;            /* the name of the region */
  70.     char *typename;        /* its category, such as "island" or "bay" */
  71.     /* something for syntax? "foo bay" vs "bay of foo" */
  72.     short superid[3];        /* indices of containing regions */
  73.     struct a_feature *next;    /* arranged in a linked list */
  74.     /* cached info about a region */
  75.     int size;
  76.     int x, y;
  77.     int mindiam;
  78.     int maxdiam;
  79.     int relabel;
  80. } Feature;
  81.  
  82. /* Paths. */
  83.  
  84. typedef struct a_waypoint {
  85.     int x, y;
  86.     int note;
  87. } Waypoint;
  88.  
  89. typedef struct a_path {
  90.     int numwps;
  91.     Waypoint wps[100];
  92. } Path;
  93.  
  94. /* Use this macro to get a area-spanning layer of the given type. */
  95.  
  96. #define malloc_area_layer(TYPE)  \
  97.   ((TYPE *) xmalloc(area.width * area.height * sizeof(TYPE)))
  98.  
  99. #define zero_area_layer(ADDR, TYPE)  \
  100.   (memset(ADDR, 0, area.width * area.height * sizeof(TYPE)))
  101.  
  102. /* General 2D malloced area array usage.  Names from Lisp. */
  103.  
  104. #define aref(m,x,y) ((m)[area.width * (y) + (x)])
  105.  
  106. #define aset(m,x,y,v) ((m)[area.width * (y) + (x)] = (v))
  107.  
  108. #define aadd(m,x,y,v) ((m)[area.width * (y) + (x)] += (v))
  109.  
  110. /* The unit is a raw pointer - this macro is used a *lot*.  This could
  111.    be space-optimized by using a 16-bit unit id. */
  112.  
  113. #define unit_at(x,y) aref(area.units, x, y)
  114.  
  115. #define set_unit_at(x,y,u) aset(area.units, x, y, u)
  116.  
  117. /* Iterate through all units in this cell (but not their occs) . */
  118.  
  119. #define for_all_stack(x,y,var)  \
  120.   for ((var) = unit_at((x), (y)); (var) != NULL;  (var) = (var)->nexthere)
  121.  
  122. /* Test if the terrain has been allocated yet. */
  123.  
  124. #define terrain_defined() (area.terrain != NULL)
  125.  
  126. /* The terrain at each cell is just the number of the terrain type. */
  127.  
  128. #define terrain_at(x,y) aref(area.terrain, x, y)
  129.  
  130. #define set_terrain_at(x,y,t) aset(area.terrain, x, y, t)
  131.  
  132. /* Auxiliary terrain array of layers. */
  133.  
  134. #define any_aux_terrain_defined() (area.auxterrain != NULL)
  135.  
  136. #define aux_terrain_defined(t)  \
  137.   (any_aux_terrain_defined() && area.auxterrain[t] != NULL)
  138.  
  139. #define aux_terrain_at(x,y,t) aref(area.auxterrain[t], x, y)
  140.  
  141. #define set_aux_terrain_at(x,y,t,v) aset(area.auxterrain[t], x, y, v)
  142.  
  143. /* Not really correct, should finish. */
  144.  
  145. #define any_borders_at(x, y, b) (aux_terrain_at(x, y, b) != 0)
  146.  
  147. #define any_connections_at(x, y, c) (aux_terrain_at(x, y, c) != 0)
  148.  
  149. /* Elevation layer. */
  150.  
  151. #define world_is_flat() (minelev == maxelev)
  152.  
  153. #define elevations_defined() (area.elevations != NULL)
  154.  
  155. #define elev_at(x,y) aref(area.elevations, x, y)
  156.  
  157. #define set_elev_at(x,y,v) aset(area.elevations, x, y, v)
  158.  
  159. /* Feature layer. */
  160.  
  161. #define features_defined() (area.features != NULL)
  162.  
  163. /* The "raw feature" is its "short" identifier. */
  164.  
  165. #define raw_feature_at(x,y) aref(area.features, x, y)
  166.  
  167. #define set_raw_feature_at(x,y,f) aset(area.features, x, y, f)
  168.  
  169. /* Population layer. */
  170.  
  171. #define people_sides_defined() (area.peopleside != NULL)
  172.  
  173. #define people_side_at(x,y) aref(area.peopleside, x, y)
  174.  
  175. #define set_people_side_at(x,y,s) aset(area.peopleside, x, y, s)
  176.  
  177. /* A cell might be entirely uninhabited, so need an extra value to indicate. */
  178.  
  179. #define NOBODY (MAXSIDES+1)
  180.  
  181. #define populated(x,y) (people_side_at(x,y) != NOBODY)
  182.  
  183. /* Array of material layers. */
  184.  
  185. #define any_cell_materials_defined() (area.materials != NULL)
  186.  
  187. #define cell_material_defined(m) (area.materials[m] != NULL)
  188.  
  189. #define material_at(x,y,m) aref(area.materials[m], x, y)
  190.  
  191. #define set_material_at(x,y,m,v) aset(area.materials[m], x, y, v)
  192.  
  193. /* Temperature layer. */
  194.  
  195. #define temperatures_defined() (area.temperature != NULL)
  196.  
  197. #define temperature_at(x,y) aref(area.temperature, x, y)
  198.  
  199. #define set_temperature_at(x,y,v) aset(area.temperature, x, y, v)
  200.  
  201. /* Clouds layer. */
  202.  
  203. #define clouds_defined() (area.clouds != NULL)
  204.  
  205. #define raw_cloud_at(x,y) aref(area.clouds, x, y)
  206.  
  207. #define set_raw_cloud_at(x,y,v) aset(area.clouds, x, y, v)
  208.  
  209. #define cloud_bottoms_defined() (area.cloudbottoms != NULL)
  210.  
  211. #define raw_cloud_bottom_at(x,y) aref(area.cloudbottoms, x, y)
  212.  
  213. #define set_raw_cloud_bottom_at(x,y,v) aset(area.cloudbottoms, x, y, v)
  214.  
  215. #define cloud_heights_defined() (area.cloudheights != NULL)
  216.  
  217. #define raw_cloud_height_at(x,y) aref(area.cloudheights, x, y)
  218.  
  219. #define set_raw_cloud_height_at(x,y,v) aset(area.cloudheights, x, y, v)
  220.  
  221. /* Winds layer. */
  222.  
  223. #define winds_defined() (area.winds != NULL)
  224.  
  225. #define raw_wind_at(x,y) aref(area.winds, x, y)
  226.  
  227. #define set_raw_wind_at(x,y,v) aset(area.winds, x, y, v)
  228.  
  229. #define wind_dir_at(x,y) (raw_wind_at(x, y) & 0x07)
  230.  
  231. #define wind_force_at(x,y) (raw_wind_at(x, y) >> 3)
  232.  
  233. #define set_wind_at(x,y,d,f) (set_raw_wind_at(x, y, ((f) << 3) | (d)))
  234.  
  235. /* Handlers for scratch layers. */
  236.  
  237. #define tmp1_at(x,y) aref(area.tmp1, x, y)
  238.  
  239. #define set_tmp1_at(x,y,v) aset(area.tmp1, x, y, v)
  240.  
  241. #define tmp2_at(x,y) aref(area.tmp2, x, y)
  242.  
  243. #define set_tmp2_at(x,y,v) aset(area.tmp2, x, y, v)
  244.  
  245. #define tmp3_at(x,y) aref(area.tmp3, x, y)
  246.  
  247. #define set_tmp3_at(x,y,v) aset(area.tmp3, x, y, v)
  248.  
  249. /* This little macro implements wraparound in the x direction. */
  250. /* The stupid add is for the benefit of brain-damaged mod operators */
  251. /* that don't handle negative numbers properly. */
  252.  
  253. /* (This could use some general cleanup) */
  254.  
  255. #define wrap(x) (area.xwrap ? (((x) + area.width) % area.width) : (x))
  256.  
  257. #define wrapx(x) (area.xwrap ? (((x) + area.width) % area.width) : (x))
  258.  
  259. /* Constrain y to northern and southern edges. */
  260.  
  261. #define limit(y) (max(0, min((y), (area.height-1))))
  262.  
  263. #define interior(y) (max(1, min((y), (area.height-2))))
  264.  
  265. #define xy_in_dir(x,y,d,nx,ny) \
  266.   (nx) = wrapx((x) + dirx[d]);  (ny) = (y) + diry[dir];
  267.  
  268. /* Iteration over all valid cell positions in a area.  These should be
  269.    used carefully, since they don't (can't) have any grouping braces
  270.    embedded. */
  271.  
  272. #define for_all_cells(x,y)  \
  273.   for (x = 0; x < area.width; x++)  \
  274.     for (y = 0; y < area.height; y++)  \
  275.       if (in_area(x, y))
  276.  
  277. /* This doesn't generate positions along area edges.  Typically more
  278.    useful within game. */
  279.  
  280. #define for_all_interior_cells(x,y)  \
  281.   for (x = 0; x < area.width; x++)  \
  282.     for (y = 1; y < area.height - 1; y++)  \
  283.       if (inside_area(x, y))
  284.  
  285. /* Returns the lighting state of a given position. */
  286. /* (should opencode distance call here) */
  287.  
  288. #define lighting(x,y,snx,sny)  \
  289.   ((distance(x, y, snx, sny) < (3 * world.circumference) / 5) ? 2 : 0)
  290.  
  291. /* True if the given x,y is dark. */
  292.  
  293. #define night_at(x,y) (daynight && lighting((x), (y), (int) sunx, (int) suny) == 0)
  294.  
  295. /* World-related variables. */
  296.  
  297. extern World world;
  298.  
  299. extern Area area;
  300.  
  301. extern int midturnrestore;
  302.  
  303. extern int numcelltypes;
  304. extern int numbordtypes;
  305. extern int numconntypes;
  306. extern int numcoattypes;
  307.  
  308. extern int minelev;
  309. extern int maxelev;
  310. extern int mintemp;
  311. extern int maxtemp;
  312. extern int minwindforce;
  313. extern int maxwindforce;
  314.  
  315. extern int any_materials_in_terrain;
  316. extern int any_temp_variation;
  317. extern int any_temp_variation_in_layer;
  318. extern int any_wind_variation;
  319. extern int any_wind_variation_in_layer;
  320. extern int any_clouds;
  321.  
  322. /* World-related functions. */
  323.  
  324. extern void init_world PROTO ((void));
  325. extern int set_world_circumference PROTO ((int circum, int warn));
  326. extern int set_area_shape PROTO ((int width, int height, int warn));
  327. extern int valid_area_shape PROTO ((int width, int height, int warn));
  328. extern void check_area_shape PROTO ((void));
  329. extern void calculate_world_globals PROTO ((void));
  330. extern void count_terrain_subtypes PROTO ((void));
  331.  
  332. extern void allocate_area_terrain PROTO ((void));
  333. extern void allocate_area_aux_terrain PROTO ((int t));
  334. extern void allocate_area_scratch PROTO ((int n));
  335. extern void allocate_area_elevations PROTO ((void));
  336. extern void allocate_area_temperatures PROTO ((void));
  337. extern void allocate_area_people_sides PROTO ((void));
  338. extern void allocate_area_material PROTO ((int m));
  339. extern void allocate_area_clouds PROTO ((void));
  340. extern void allocate_area_cloud_altitudes PROTO ((void));
  341. extern void allocate_area_cloud_bottoms PROTO ((void));
  342. extern void allocate_area_cloud_heights PROTO ((void));
  343. extern void allocate_area_winds PROTO ((void));
  344.  
  345. extern int fn_terrain_at PROTO ((int x, int y));
  346. extern int fn_aux_terrain_at PROTO ((int x, int y));
  347. extern int fn_feature_at PROTO ((int x, int y));
  348. extern int fn_elevation_at PROTO ((int x, int y));
  349. extern int fn_people_side_at PROTO ((int x, int y));
  350. extern int fn_material_at PROTO ((int x, int y));
  351. extern int fn_temperature_at PROTO ((int x, int y));
  352. extern int fn_raw_cloud_at PROTO ((int x, int y));
  353. extern int fn_raw_cloud_bottom_at PROTO ((int x, int y));
  354. extern int fn_raw_cloud_height_at PROTO ((int x, int y));
  355. extern int fn_raw_wind_at PROTO ((int x, int y));
  356.  
  357. extern void fn_set_terrain_at PROTO ((int x, int y, int val));
  358. extern void fn_set_aux_terrain_at PROTO ((int x, int y, int val));
  359. extern void fn_set_people_side_at PROTO ((int x, int y, int val));
  360. extern void fn_set_raw_feature_at PROTO ((int x, int y, int val));
  361. extern void fn_set_elevation_at PROTO ((int x, int y, int val));
  362. extern void fn_set_material_at PROTO ((int x, int y, int val));
  363. extern void fn_set_temperature_at PROTO ((int x, int y, int val));
  364. extern void fn_set_raw_wind_at PROTO ((int x, int y, int val));
  365. extern void fn_set_raw_cloud_at PROTO ((int x, int y, int val));
  366. extern void fn_set_raw_cloud_bottom_at PROTO ((int x, int y, int val));
  367. extern void fn_set_raw_cloud_height_at PROTO ((int x, int y, int val));
  368.  
  369. extern int search_around PROTO ((int x0, int y0, int maxdist,
  370.                  int (*pred)(int, int),
  371.                  int *rxp, int *ryp, int incr));
  372. extern int search_and_apply PROTO ((int x0, int y0, int maxdist,
  373.                     int (*pred)(int, int),
  374.                     int *rxp, int *ryp, int incr,
  375.                     void (*fn)(int, int), long num));
  376. extern void apply_to_area PROTO ((int x0, int y0, int dist,
  377.                   void (*fn)(int, int)));
  378. extern void apply_to_area_plus_edge PROTO ((int x0, int y0, int dist,
  379.                         void (*fn)(int, int)));
  380. extern void apply_to_ring PROTO ((int x0, int y0, int distmin, int distmax,
  381.                   void (*fn)(int, int)));
  382. extern void apply_to_hexagon PROTO ((int x0, int y0, int w2, int h2,
  383.                      void (*fn)(int, int)));
  384. extern void apply_to_path PROTO ((int fx, int fy, int tx, int ty,
  385.                   int (*dirfn)(int, int, int *, int),
  386.                   int (*fn)(int, int, int, int, int),
  387.                   int shortest));
  388. int find_path PROTO ((int fx, int fy, int tx, int ty,
  389.               int (*chooser)(int, int, int, int, int *),
  390.               int maxwps, Waypoint *waypoints, int *numwpsp));
  391.  
  392. extern int in_area PROTO ((int x, int y));
  393. extern int inside_area PROTO ((int x, int y));
  394. extern int area_cells PROTO ((void));
  395. extern int border_at PROTO ((int x, int y, int dir, int t));
  396. extern void set_border_at PROTO ((int x, int y, int dir, int t, int onoff));
  397. extern int connection_at PROTO ((int x, int y, int dir, int t));
  398. extern void set_connection_at PROTO ((int x, int y, int dir, int t, int onoff));
  399. extern void patch_linear_terrain PROTO ((int t));
  400. extern void init_features PROTO ((void));
  401. extern Feature *create_feature PROTO ((char *typename, char *name));
  402. extern Feature *find_feature PROTO ((int fid));
  403. extern Feature *feature_at PROTO ((int x, int y));
  404. extern void set_feature_type_name PROTO ((Feature *feature, char *typename));
  405. extern void set_feature_name PROTO ((Feature *feature, char *name));
  406. extern void destroy_feature PROTO ((Feature *feature));
  407. extern void renumber_features PROTO ((void));
  408. extern void compute_all_feature_centroids PROTO ((void));
  409. extern void compute_feature_centroid PROTO ((Feature *feature));
  410.  
  411. extern int point_in_dir PROTO ((int x, int y, int dir, int *xp, int *yp));
  412. extern int interior_point_in_dir PROTO ((int x, int y, int dir, int *xp, int *yp));
  413. extern int point_in_dir_n PROTO ((int x, int y, int dir, int n, int *xp, int *yp));
  414. extern int random_point PROTO ((int *xp, int *yp));
  415. extern int random_point_near PROTO ((int cx, int cy, int radius, int *xp, int *yp));
  416. extern int random_point_in_area PROTO ((int cx, int cy, int rx, int ry, int *xp, int *yp));
  417. extern void terrain_subtype_warning PROTO ((char *context, int t));
  418. extern int approx_dir PROTO ((int dx, int dy));
  419. extern int hextant PROTO ((int dx, int dy));
  420. extern int distance PROTO ((int x1, int y1, int x2, int y2));
  421. extern int closest_dir PROTO ((int x, int y));
  422.  
  423. #ifdef DESIGNERS
  424.  
  425. extern void paint_cell PROTO ((Side *side, int x, int y, int r, int t));
  426. extern void paint_border PROTO ((Side *side, int x, int y, int dir, int t, int mode));
  427. extern void paint_connection PROTO ((Side *side, int x, int y, int dir, int t, int mode));
  428. extern void paint_coating PROTO ((Side *side, int x, int y, int r, int t, int depth));
  429. extern void paint_people PROTO ((Side *side, int x, int y, int r, int s));
  430. extern void paint_feature PROTO ((Side *side, int x, int y, int r, int f));
  431. extern void paint_elevation PROTO ((Side *side, int x, int y, int r, int elev));
  432. extern void paint_temperature PROTO ((Side *side, int x, int y, int r, int temp));
  433. extern void paint_material PROTO ((Side *side, int x, int y, int r, int m, int amt));
  434. extern void paint_clouds PROTO ((Side *side, int x, int y, int r, int cloudtype, int bot, int hgt));
  435. extern void paint_winds PROTO ((Side *side, int x, int y, int r, int dir, int force));
  436.  
  437. #endif /* DESIGNERS */
  438.